package com.nobuyme.box.toy;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.nobuyme.box.R;
import com.nobuyme.box.account.Account;
import com.nobuyme.box.account.AccountManager;
import com.nobuyme.box.app.UrlCenter;
import com.nobuyme.box.bean.AdminStatusEntity;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.framework.httprequest.HttpRequest;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.pachong.android.frameworkbase.utils.EasyToast;

import butterknife.Bind;
import butterknife.OnClick;

public class ManageHomeActivity extends BaseActivity {
    @Bind(R.id.iv_manage_home_back)
    ImageView mIvManageHomeBack;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideToolbar();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_manage_home, null);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, ManageHomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        context.startActivity(intent);
    }

    @OnClick({R.id.iv_manage_up, R.id.iv_manage_recy, R.id.iv_manage_down,R.id.iv_manage_home_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_manage_up:
                //需要上货
                ToyManageListActivity.start(this,0);
                break;
            case R.id.iv_manage_recy:
                //需要回收
                ToyManageListActivity.start(this,2);
                break;
            case R.id.iv_manage_down:
                //待租赁，下架
                ToyManageListActivity.start(this,1);
                break;
            case R.id.iv_manage_home_back:
                //返回时变更管理员状态
                logout();
                break;
        }
    }
    private void logout() {
        Account acc = AccountManager.getCurrentAccount();
        HttpRequest request = new HttpRequest(this);
        String urlAll = String.format(UrlCenter.ADMIN_LOGOUT,acc.getId());
        request.put(urlAll, null, new DataRequestListener<AdminStatusEntity>(AdminStatusEntity.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }
            @Override
            public void onSuccess(AdminStatusEntity o) {
                super.onSuccess(o);
                EasyToast.showToast(ManageHomeActivity.this,"退出成功");
                AccountManager.getCurrentAccount().setIs_login(false);
                finish();
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                EasyToast.showToast(ManageHomeActivity.this,strMsg);
            }
        });

    }


}
